package com.hcss.common.redis.base.impl;

import com.hcss.common.redis.base.RedisSetDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Repository;

import java.util.Set;

/**
 * @描述: redis Set类型数据操作Dao接口实现
 * @作者: xuqian
 * @创建日期: 2018-08-07 11:57
 * @公司 浙江鸿程计算机系统有限公司
 */
@Repository("redisSetDao")
public class RedisSetDaoImpl<T> extends BaseRedisDaoImpl<T> implements RedisSetDao<T> {
    private final SetOperations<String, T> setOperations;

    @Autowired
    public RedisSetDaoImpl(RedisTemplate redisTemplate) {
        super(redisTemplate);
        this.setOperations = redisTemplate.opsForSet();
    }

    @Override
    public void insert(String key, T value) {
        setOperations.add(key, value);
    }

    /**
     * 默认从队列首部开始取一个数据
     * @param key
     * @return
     */
    @Override
    public T selectObject(String key) {
        return setOperations.pop(key);
    }

    @Override
    public SetOperations getSetOperations() {
        return setOperations;
    }

    @Override
    public void insert(String key, T... values) {
        setOperations.add(key, values);
    }

    @Override
    public Set<T> selectAll(String key) {
        return setOperations.members(key);
    }

    @Override
    public boolean contains(String key, T value) {
        return setOperations.isMember(key, value);
    }

    @Override
    public void delete(String key, T... values) {
        setOperations.remove(key, values);
    }

    @Override
    public void deleteAll(String key) {
        super.clear(key);
    }

    @Override
    public Set<T> getDiff(String key1, String key2) {
        return setOperations.difference(key1, key2);
    }

    @Override
    public Set<T> getDiffAndStore(String key1, String key2, String key3) {
        setOperations.differenceAndStore(key1, key2, key3);
        return selectAll(key3);
    }

    @Override
    public Set<T> getInter(String key1, String key2) {
        return setOperations.intersect(key1, key2);
    }

    @Override
    public Set<T> getInterAndStore(String key1, String key2, String key3) {
        setOperations.intersectAndStore(key1, key2, key3);
        return selectAll(key3);
    }

    @Override
    public Set<T> getUnion(String key1, String key2) {
        return setOperations.union(key1, key2);
    }

    @Override
    public Set<T> getUnionAndStore(String key1, String key2, String key3) {
        setOperations.unionAndStore(key1, key2, key3);
        return selectAll(key3);
    }
}
